/**
 * Created by zhangwx on 2015/10/14.
 */
// JavaScript Document
/*
 */
(function($) {

    $.selectSort = function(options){install(options)};
    //各种属性
    $.selectSort.defaults = {
        title:"",//显示的标题
        divClass: "", // 显示的div
        clickClass:"",//判断你点击的class
        obj: "", // 你要查询json的对象
        code:"",
        name: "", // obj下的name
        value: "", // obj下的value
        okEvent: function (){},//关闭层之后调用的方法，也可不写。
        divBox: false //true就显示，false就不显示. 默认false
    }
    function install(options){
        // 合并参数
        var options = $.extend($.selectSort.defaults, options);
        //页面显示
        $(options.divClass).slideDown(500);
        var proList = "";
        var html = "";
        var divBox ="";
        var shopSort ="";
        var title ="";
        if(options.divBox == true){
           // divBox = "<div class='cityTitle'><span class='fl'>"+options.title+"</span><!--<img src='../images/closed.gif' class='showImg'/>--></div><div class='proList'></div><ul class='proEngAll'><li><a href='javascript:void(0)' class='anchor'>A</a></li><li><a href='javascript:void(0)' class='anchor'>B</a></li><li><a href='javascript:void(0)' class='anchor'>C</a></li><li><a href='javascript:void(0)' class='anchor'>F</a></li><li><a href='javascript:void(0)' class='anchor'>G</a></li><li><a href='javascript:void(0)' class='anchor'>H</a></li><li><a href='javascript:void(0)' class='anchor'>J</a></li><li><a href='javascript:void(0)' class='anchor'>L</a></li><li><a href='javascript:void(0)' class='anchor'>N</a></li><li><a href='javascript:void(0)' class='anchor'>Q</a></li><li><a href='javascript:void(0)' class='anchor'>S</a></li><li><a href='javascript:void(0)' class='anchor'>X</a></li><li><a href='javascript:void(0)' class='anchor'>Y</a></li><li><a href='javascript:void(0)' class='anchor'>Z</a></li></ul><div class='proEngSort'></div>";
            divBox = "<div class='cityTitle'><span class='fl'>"+options.title+":"+$(".click").val()+"</span><!--<img src='../images/closed.gif' class='showImg'/>--></div><div class='proList'></div>" +
                "" +
                "<div class='proEngAll'><a href='javascript:void(0)' class='anchor'>A</a><a href='javascript:void(0)' class='anchor'>B</a><a href='javascript:void(0)' class='anchor'>C</a><a href='javascript:void(0)' class='anchor'>F</a><a href='javascript:void(0)' class='anchor'>G</a><a href='javascript:void(0)' class='anchor'>H</a><a href='javascript:void(0)' class='anchor'>J</a><a href='javascript:void(0)' class='anchor'>L</a><a href='javascript:void(0)' class='anchor'>N</a><a href='javascript:void(0)' class='anchor'>Q</a><a href='javascript:void(0)' class='anchor'>S</a><a href='javascript:void(0)' class='anchor'>X</a><a href='javascript:void(0)' class='anchor'>Y</a><a href='javascript:void(0)' class='anchor'>Z</a></ul>" +
                "<div class='proEngSort'></div>";
        }else{
            divBox ="<div class='cityTitle'><span class='fl'>"+options.title+":"+$(".click").val()+"</span></div><div class='proList'></div><div class='shopSort'></div>";
        }
        $(divBox).appendTo(options.divClass);
        var objN = eval("(" + options.obj + ")");
        for(n in objN) {
            if(objN[n]["type"]!="SI") {

                html += "<h4 class='city_" + objN[n][options.code] + "'>" + objN[n][options.name] + "</h4>";
                shopSort +="<h4 class='city_" + objN[n][options.code] + "'>" + objN[n][options.name] + "</h4>";

                for (var j = 0; j < objN[n][options.value].length; j++) {
                    html += '<a href="javascript:void(0);" id="' + objN[n][options.value][j][options.code] + '">' + objN[n][options.value][j][options.name] + '</a>';
                    shopSort += '<a href="javascript:void(0);" id="' + objN[n][options.value][j][options.code] + '">' + objN[n][options.value][j][options.name] + '</a>';
                }

            }
            //由省列表改為直轄市列表
            if(objN[n]["type"]=="SI"){
                proList +='<a href="javascript:void(0);" id="city_'+objN[n][options.name]+'_'+n+'">' + objN[n][options.name]+ '</a>';
            }

        }
        //把得到的值，放到指定的div中去
        $(html).appendTo(".proEngSort");
        $(proList).appendTo(".proList");
        $(shopSort).appendTo(".shopSort");

        $(".proEngSort a,.proList a,.shopSort a").bind("click",function(){

            var proCity = $(this).html();//市
            var proId = $(this).attr("id");
            $(options.clickClass).val(proCity);
            $("#proId").val(proId);//获得value值。
            $(options.divClass).slideUp("fast", options.okEvent).empty();//绑定事件，用于获得值后触发，

        });

        $(".anchor").on("click",function(){
            $(".proEngSort").scrollTop(0);
            var anchorText = $(this).text();
            if($(".city_"+anchorText).length > 0){
                var pos =  $(".city_"+anchorText).offset().top;
                var poshigh = $(".cityTitle").height();
                $(".proEngSort").animate({ scrollTop: pos-poshigh-120},0);
            }
            return false;
        });
    }
})(jQuery);